<?php

/**
 * 自动生成 MerPromoteStatService
 * 
 * @filepath  Apps/Common/Service/MerPromoteStatService.class.php
 * @author  gt_admin 
 * @version v1.0 , 2015-09-23
 */
namespace Common\Service;


class MerPromoteStatService {
  
  private static $instance;

  public static function instance() {
    if (self::$instance == null) {
      $instance = new MerPromoteStatService();
    }

    return $instance;
  }
  
  function get_default_row() {
    return array(
        'id' => '' ,  
'date' => '' ,  
'sub_num' => '0' ,  

    );
  }

  function get_by_id( $id ){
    $MerPromoteStat = M('MerPromoteStat');
    $data = $MerPromoteStat->find( $id ) ;
    return $data ? $data : array();
  }
  
  function get_by_cond( $config ) {
    $default = array(
      'start_time' => '',
      'end_time' => '',
      'page' => 1,
      'page_size' => 6,
      'status' => '' ,
      'count' => FALSE,
      'order' => 'DESC' ,
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $MerPromoteStat = M('MerPromoteStat');
    
    $where = array();
    
    if ( !empty($config['status']) ) {
      $where['status'] = $config['status'] ;
    }
    
    if ( !empty( $config['start_time'] ) ) {
      $where['date'][] = array('egt' , $config['start_time']);
    }
    
    if ( !empty( $config['end_time'] ) ) {
      $where['date'][] = array('elt' , $config['end_time']);
    } 
    
    if ($config['count']) {
      return $MerPromoteStat->where($where)->count() ;
    } else {
      $order = $config['sort'] . ' ' . $config['order'] ;
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $MerPromoteStat
              ->where($where)
              ->limit($limit)->order( $order )->select();
      //echo $MerPromoteStat->_sql();
      return $data ? $data : array();
    }
  }
  
  function count_by_config( $config ) {
    if ( !empty( $config['start_time'] ) ) {
      $where['date'][] = array('egt' , $config['start_time']);
    }
    if ( !empty( $config['end_time'] ) ) {
      $where['date'][] = array('elt' , $config['end_time']);
    }
    
    $MerUserPromoteStat = M('MerUserPromoteStat');
    $total = $MerUserPromoteStat->where($where )->sum('sub_num');
    //echo $MerUserPromoteStat->_sql();
    return $total ;
  }
  
  function create( $data ) {
    $MerPromoteStat = M('MerPromoteStat');
    $ret = $MerPromoteStat->add($data);
    if ($ret === FALSE ) {
      return ajax_arr('添加失败', FALSE);
    } else {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    }
  }

  function update( $id , $data ) {
    $MerPromoteStat = M('MerPromoteStat');
    
    $ret = $MerPromoteStat->where("id = %d", $id)->save($data);
    if ( $ret === FALSE ) {
      return ajax_arr('编辑失败', FALSE);
    } else {
      return ajax_arr('编辑成功', TRUE);
    }
  }
  
  function delete( $ids ) {
    $MerPromoteStat = M('MerPromoteStat');
    $ret = $MerPromoteStat->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
}
